package com.zhonghan.recharge.service.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhonghan.common.bean.Result;
import com.zhonghan.common.enums.ResultStatus;
import com.zhonghan.common.utils.StringUtils;
import com.zhonghan.recharge.dao.WechatAccountDao;
import com.zhonghan.recharge.pojo.WechatAccount;
import com.zhonghan.recharge.service.WechatAccountService;
@Service
public class WechatAccountServiceImpl implements WechatAccountService{
	
	private Logger logger = Logger.getLogger(WechatAccountService.class);
	
    @Autowired
    private WechatAccountDao wechatAccountDao;
    
    @Override
    public long getWechatAccountRowCount(){
        return wechatAccountDao.getWechatAccountRowCount();
    }
    @Override
    public List<WechatAccount> selectWechatAccount(){
        return wechatAccountDao.selectWechatAccount();
    }
    @Override
    public List<WechatAccount> selectWechatAccountByObj(WechatAccount obj){
        return wechatAccountDao.selectWechatAccountByObj(obj);
    }
    @Override
    public WechatAccount selectWechatAccountById(Integer id){
        return wechatAccountDao.selectWechatAccountById(id);
    }
    @Override
    public int insertWechatAccount(WechatAccount value){
        return wechatAccountDao.insertWechatAccount(value);
    }
    @Override
    public int insertNonEmptyWechatAccount(WechatAccount value){
        return wechatAccountDao.insertNonEmptyWechatAccount(value);
    }
    @Override
    public int insertWechatAccountByBatch(List<WechatAccount> value){
        return wechatAccountDao.insertWechatAccountByBatch(value);
    }
    @Override
    public int deleteWechatAccountById(Integer id){
        return wechatAccountDao.deleteWechatAccountById(id);
    }
    @Override
    public int updateWechatAccountById(WechatAccount enti){
        return wechatAccountDao.updateWechatAccountById(enti);
    }
    @Override
    public int updateNonEmptyWechatAccountById(WechatAccount enti){
        return wechatAccountDao.updateNonEmptyWechatAccountById(enti);
    }

    public WechatAccountDao getWechatAccountDao() {
        return this.wechatAccountDao;
    }

    public void setWechatAccountDao(WechatAccountDao wechatAccountDao) {
        this.wechatAccountDao = wechatAccountDao;
    }
	@Override
	public Result saveAccount(String name, String originalId, String appid, String appsecret, String token) {
		
		WechatAccount wa = new WechatAccount();
		Result result = new Result();
		if(StringUtils.isNotEmpty(name)){
			wa.setName(name);
		}else{
			result.setCode(ResultStatus.FAILURE.getCode());
			result.setMsg("Parameters are not allowed to be empty: name");
			return result;
		}
		if(StringUtils.isNotEmpty(originalId)){
			wa.setOriginalId(originalId);
		}else{
			result.setCode(ResultStatus.FAILURE.getCode());
			result.setMsg("Parameters are not allowed to be empty: originalId");
			return result;
		}
		if(StringUtils.isNotEmpty(appid)){
			wa.setAppid(appid);
		}else{
			result.setCode(ResultStatus.FAILURE.getCode());
			result.setMsg("Parameters are not allowed to be empty: appid");
			return result;
		}
		if(StringUtils.isNotEmpty(appsecret)){
			wa.setAppsecret(appsecret);
		}else{
			result.setCode(ResultStatus.FAILURE.getCode());
			result.setMsg("Parameters are not allowed to be empty: appsecret");
			return result;
		}
		if(StringUtils.isNotEmpty(token)){
			wa.setToken(token);
		}else{
			result.setCode(ResultStatus.FAILURE.getCode());
			result.setMsg("Parameters are not allowed to be empty: token");
			return result;
		}
		
		
		try {
			List<WechatAccount> list = wechatAccountDao.selectWechatAccount();
			// 如果数据库有数据则更新
			if(list!=null && list.size()>0){
				WechatAccount wechatAccount = list.get(0);
				wa.setId(wechatAccount.getId());
				wechatAccountDao.updateNonEmptyWechatAccountById(wa);
				logger.info("更新微信公众号账户信息: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			}else{
				//否则就添加一条数据
				wechatAccountDao.insertNonEmptyWechatAccount(wa);
				logger.info("新增微信公众号账户信息: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			}
			
			result.setCode(ResultStatus.SUCCESS.getCode());
			result.setMsg("微信公众号账户信息保存成功!");
			
		} catch (Exception e) {
			result.setCode(ResultStatus.BUSYNESS.getCode());
			result.setMsg(ResultStatus.BUSYNESS.getDescription());
			e.printStackTrace();
			
		}
		
		return result;
	}

}